<?php
/**
 * @package: Asalsuri
 * @subpackage: Club sponsor Model
 * @author:  Pham Van Diep
 * @date: 19/2/1024
 * 
*/
class Sponsor_Model extends CI_Model{
	var $database;
	public function __construct() {
        parent::__construct();
		$this->database = $this->db->conn_id;
		
	}
    /**
     * @author: Pham Van Diep
     * @description: create object sponsor
     * @param array $row: array sponsor
     * @return object | null
    */
	private function _sponsorToObj($row) {
		if(isset($row)) {
			$objsponsor			            = new stdClass();
			$objsponsor->sponsorid 	    	= isset($row['sponsorid'])   ? $row['sponsorid']   : 0;
            $objsponsor->clubid 		    = isset($row['clubid'])      ? $row['clubid']      : 0;            
			$objsponsor->name               = isset($row['name'])        ? $row['name']        : '';
			$objsponsor->name_se            = isset($row['name_se'])     ? $row['name_se']     : '';
			$objsponsor->logo 	            = isset($row['logo'])        ? $row['logo']        : '';
			$objsponsor->description	    = isset($row['description']) ? $row['description'] : '';
            $objsponsor->short	            = isset($row['short'])       ? $row['short']       : '';
            $objsponsor->tag	            = isset($row['tag'])         ? $row['tag']         : '';
            $objsponsor->status	            = isset($row['status'])      ? $row['status']      : 0;
            $objsponsor->created	        = isset($row['created'])     ? $row['created']     : '';
            $objsponsor->modified	        = isset($row['modified'])    ? $row['modified']    : '';
            $objsponsor->logid	            = isset($row['logid'])       ? $row['logid']       : '';                  
			return $objsponsor;
		}
		return null;
	}
    /**
     * @author: Pham Van Diep
     * @description: get sponsor info
     * @param integer $intId: ID
     * @return object | null
    */
	public function get($intId) {	
		$stmt = $this->database->prepare('CALL clubs_sponsor_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->_sponsorToObj($stmt->fetch());				
	}
    
    /**
    * @author: Pham Van Diep
    * @description: get all club sponsor
    * @param integer $intLoginid: user login id
    * @param integer $intStatus:
    *                            0: inactive
    *                           1: active
    *                           2: suspend report
    *                           3: rejected
    *                           4: member delete
    *                           5: leader delete 
    *                           6: admin delete                          
    * @param integer $intClubid: club id
    * @param integer $intOption :
    *                               0:ALL
    *                               1:?
    * @param datetime $dStart : get from start date
    * @param datetime $dEnd : get to date 
    * @param integer $intOffset: row start paging 
    * @param integer $intLimit: display row on paging 
    * @param integer &$intTotal : return num rows of result   
    * @param array
    */       
    public function getAll($intLoginid,$intStatus,$intClubid,$intOption,$strKeyword,$dStart,$dEnd,$intOffset,$intLimit,&$intTotal=0) {
		$stmt = $this->database->prepare('CALL clubs_sponsor_getall_a(?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT, 11);
		$stmt->bindParam(2, $intStatus,      PDO::PARAM_INT, 4);
		$stmt->bindParam(3, $intClubid,      PDO::PARAM_INT, 11);
        $stmt->bindParam(4, $intOption,      PDO::PARAM_INT,4);
        $stmt->bindParam(5, $strKeyword,     PDO::PARAM_STR,50);
        $stmt->bindParam(6, $dStart,         PDO::PARAM_STR);
        $stmt->bindParam(7, $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(8, $intOffset,      PDO::PARAM_INT, 11);
        $stmt->bindParam(9, $intLimit,       PDO::PARAM_INT, 6);		
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
		/**
     * @author: Pham Van Diep
	 * @description: add club sponsor info  
	 * @param interger $intLoginId: id admin login (as root id width insert permission)
	 * @param integer  $intClubId: club id	 
	 * @param string   $strName: name
     * @param string   $strLogo:  sponsor logo
     * @param string   $strShort:  short description
     * @param string   $strDescription:  sponsor description
   	 * @param string   $strTag:  tag
     * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION ADD
     *      	
	 */    
	public function add($intLoginId,$intClubId, $strName, $strLogo, $strShort,$strDescription, $strTag) {
		$stmt = $this->database->prepare('CALL clubs_sponsor_add(?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,       PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intClubId,        PDO::PARAM_INT, 11);		       
		$stmt->bindParam(3, $strName,          PDO::PARAM_STR, 100);
        $stmt->bindParam(4, $strLogo,          PDO::PARAM_STR, 150);		
		$stmt->bindParam(5, $strShort,         PDO::PARAM_STR, 255);		
		$stmt->bindParam(6, $strDescription,   PDO::PARAM_STR);
        $stmt->bindParam(7, $strTag,           PDO::PARAM_STR, 150);        	
		$stmt->execute();
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
	/**
     * @author: Pham Van Diep
	 * @description: update club sponsor info  
	 * @param interger $intLoginId: id admin login (as root id width insert permission)
     * @param integer  $intSponsorId: sponsor id
	 * @param integer  $intClubId: club id	 
	 * @param string   $strName: name
     * @param string   $strLogo:  sponsor logo
     * @param string   $strShort:  short description
     * @param string   $strDescription:  sponsor description
   	 * @param string   $strTag:  tag
     * @return integer
     *    0: INSERT SUCCESS
     *    1: ERROR - NOT PERMISSION UPDATE
     *      	
	 */             
	public function update($intLoginId,$intSponsorId,$intClubId, $strName, $strLogo, $strShort,$strDescription, $strTag ) {
		$stmt = $this->database->prepare('CALL clubs_sponsor_update(?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginId,       PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intSponsorId,     PDO::PARAM_INT, 11);
        $stmt->bindParam(3, $intClubId,        PDO::PARAM_INT, 11);		       
		$stmt->bindParam(4, $strName,          PDO::PARAM_STR, 100);
        $stmt->bindParam(5, $strLogo,          PDO::PARAM_STR, 150);		
		$stmt->bindParam(6, $strShort,         PDO::PARAM_STR, 255);		
		$stmt->bindParam(7, $strDescription,   PDO::PARAM_STR);
        $stmt->bindParam(8, $strTag,           PDO::PARAM_STR, 150);  	
		$stmt->execute();        
		$result = $stmt->fetch();
		return isset($result['errCode']) ? $result['errCode'] : -1;	
	}
    /**
    * @author: Pham Van Diep    
    * @description: update sponsor status 
    * @param integer $intLoginid: user login id
    * @param integer $intSponsorid: sponsor id
    * @param integer $intStatus: club status
    * @param integer $intPerm: permission      
    * @param integer
    */
    public function updateStatus($intLoginid,$intSponsorid,$intStatus,$intPerm){
        $stmt = $this->database->prepare('CALL clubs_sponsor_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,  PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intSponsorid,PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus,   PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm,     PDO::PARAM_INT, 4);							
		$stmt->execute();	
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;	
    }	

    /**
    * @author: Pham Van Diep    
    * @description: delete club
    * @param integer $intLoginid: user login id
    * @param integer $intSponsorid: sponsor id     
    * @param integer
    */
    public function delete($intLoginid,$intSponsorid){
        $stmt = $this->database->prepare('CALL clubs_sponsor_delete(?,?)');
		$stmt->bindParam(1, $intLoginid,    PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intSponsorid,  PDO::PARAM_INT, 11);
		$stmt->execute();
        $result = $stmt->fetch();
        return isset($result['errCode']) ? $result['errCode'] : -1;			
    }
   	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	public function getErrorCode ( $tableData) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):0;
	}	
	
}